﻿Imports System.Data.SqlClient

Public Class frmCatalogoAutobuses
    Dim Bitacora As New clsActividad
    Dim Usuario As New clsUsuarioPrincipal
    Private mstrCondicion As String
    Public WriteOnly Property Condicion() As String
        Set(ByVal value As String)
            mstrCondicion = value
        End Set
    End Property

    Private Sub frmCatalogoAutobuses_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Bitacora.RegistraActividad("Cerró al catálogo de Autobuses")
        Usuario.Ubicacion("Ventana Principal")
    End Sub
    Private Sub frmCatalogoAutobuses_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ToolStrip1.Cursor = Cursors.Hand
        ToolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System

        dgAutobuses.DefaultCellStyle.Font = New Font("Segoe UI", 10, FontStyle.Regular)

        Bitacora.RegistraActividad("Ingresó al catálogo de Autobuses")
        Usuario.Ubicacion("Catálogo de Autobuses")

        txtBuscar.Clear()
        CargaAutobuses()
        txtBuscar.Focus()
        dgAutobuses.Columns(2).ReadOnly = False
        dgAutobuses.Columns(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        dgAutobuses.Columns(6).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
    End Sub
    Sub CargaAutobuses()
        Windows.Forms.Cursor.Current = Cursors.WaitCursor
        dgAutobuses.Rows.Clear()

        Dim Filtro As String
        Filtro = UCase(txtBuscar.Text)
        Filtro = "%" & Filtro.Replace(" ", "%") & "%"

        Dim cnConn As New SqlConnection
        Dim strSql As String
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader

        Try
            cnConn.ConnectionString = CitraConnection
            strSql = "SELECT IdAutobus,NoEconomico,Nombre,cCodigoC01,cIdClien01,Cod_Vehiculo,Estatus FROM Autobuses A, Rutas R WHERE A.IdRuta=R.IdRuta AND (UPPER(cCodigoC01) LIKE '" & Filtro & "' " & mstrCondicion & " OR UPPER(NoEconomico) LIKE '" & Filtro & "' " & mstrCondicion & " OR UPPER(Nombre) LIKE '" & Filtro & "' " & mstrCondicion & ") AND ESTATUS=1"

            cmdBuscar.Connection = cnConn
            cmdBuscar.CommandText = strSql


            cnConn.Open()
            rdBuscar = cmdBuscar.ExecuteReader

            Dim AdminPAQ As String
            Dim OptoControl As String
            Dim estatus As String = ""

            Do While rdBuscar.Read()
                If rdBuscar("cIdClien01") > 0 Then
                    AdminPAQ = "Si"
                Else
                    AdminPAQ = "No"
                End If
                If rdBuscar("Cod_Vehiculo") <> "" Then
                    OptoControl = "Si"
                Else
                    OptoControl = "No"
                End If
                If rdBuscar("Estatus") = 1 Then
                    estatus = "Activo"
                Else
                    estatus = "Inactivo"
                End If
                dgAutobuses.Rows.Add(rdBuscar("IdAutobus"), False, rdBuscar("cCodigoC01").ToString.Trim, rdBuscar("NoEconomico").ToString.Trim, rdBuscar("Nombre"), AdminPAQ, OptoControl, estatus)
            Loop
        Catch ex As Exception
            MessageBox.Show(ex.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End Try
    End Sub

    Private Sub DesdeAdminPAQToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles DesdeAdminPAQToolStripMenuItem.Click
        frmCatalogoAutobusesImportar.ShowDialog()

    End Sub

    Private Sub AdminPAQToolStripMenuItem1_Click(sender As System.Object, e As System.EventArgs) Handles AdminPAQToolStripMenuItem1.Click
        frmCatalogoAutobusesImportar.ShowDialog()
    End Sub

    Private Sub ToolStripButton1_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton1.Click
        Close()

    End Sub

    Private Sub txtBuscar_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtBuscar.TextChanged
        CargaAutobuses()

    End Sub

    Private Sub dgAutobuses_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgAutobuses.CellContentClick

    End Sub
    Private Sub dgAutobuses_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgAutobuses.DoubleClick
        Explorar()
    End Sub

    Private Sub dgAutobuses_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles dgAutobuses.KeyPress
        Explorar()
    End Sub
    Sub Explorar()
        Dim valorCell As Integer
        Dim valorRow As DataGridViewRow

        valorCell = dgAutobuses.CurrentCell.ColumnIndex
        valorRow = dgAutobuses.CurrentRow

        AutobusSelect = dgAutobuses.Rows(valorRow.Index).Cells(2).Value.ToString.Trim

        frmCatalogoAutobusesDetalle.ShowDialog()
    End Sub

    Private Sub ToolStripButton3_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton3.Click
        Explorar()

    End Sub

    Private Sub ExaminarRegistroToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExaminarRegistroToolStripMenuItem.Click
        Explorar()

    End Sub

    Private Sub RutasToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs)

    End Sub
    Sub ImprimeFiltro()
        Dim xl As Object
        Dim wb As Object
        Dim i As Integer
        xl = CreateObject("Excel.Application")
        xl.Visible = True
        xl.Workbooks.Add()
        wb = xl.activeworkbook


        wb.SHEETS(1).cells(1, 1).value = "Imprimiendo..."
        wb.SHEETS(1).columns(1).columnwidth = 15

        Dim rng As Integer = 5

        For i = 2 To 6
            wb.SHEETS(1).cells(rng, i).FONT.name = "Segoe UI Light"
            wb.SHEETS(1).cells(rng, i).font.bold = True
            wb.SHEETS(1).CELLS(rng, i).HorizontalAlignment = -4108
            wb.SHEETS(1).CELLS(rng, i).VerticalAlignment = -4108
            wb.SHEETS(1).CELLS(rng, i).WrapText = True
            wb.SHEETS(1).CELLS(rng, i).Orientation = 0
            wb.SHEETS(1).CELLS(rng, i).AddIndent = False
            wb.SHEETS(1).CELLS(rng, i).IndentLevel = 0
            wb.SHEETS(1).CELLS(rng, i).ShrinkToFit = False
            wb.SHEETS(1).CELLS(rng, i).MergeCells = False
            wb.SHEETS(1).CELLS(rng, i).ReadingOrder = -5002
        Next
        wb.SHEETS(1).cells(rng, 2).value = "No. Interno"
        wb.SHEETS(1).cells(rng, 3).value = "No. Economico"
        wb.SHEETS(1).cells(rng, 4).value = "Ruta"
        wb.SHEETS(1).cells(rng, 5).value = "Interfaz AdminPAQ"
        wb.SHEETS(1).cells(rng, 6).value = "Interfaz OptoControl"
        wb.SHEETS(1).cells(rng, 7).value = "Estatus"
        wb.SHEETS(1).columns(2).columnwidth = 10
        wb.SHEETS(1).columns(3).columnwidth = 12
        wb.SHEETS(1).columns(4).columnwidth = 30
        wb.SHEETS(1).columns(5).columnwidth = 12
        wb.SHEETS(1).columns(6).columnwidth = 14
        wb.SHEETS(1).columns(7).columnwidth = 12

        rng = rng + 1
        For i = 0 To dgAutobuses.Rows.Count - 1
            For j = 2 To 6
                wb.SHEETS(1).cells(rng, j).FONT.name = "Segoe UI Light"

            Next
            wb.SHEETS(1).cells(rng, 2).value = dgAutobuses.Rows(i).Cells(2).Value.ToString.Trim
            wb.SHEETS(1).cells(rng, 3).value = dgAutobuses.Rows(i).Cells(3).Value.ToString.Trim
            wb.SHEETS(1).cells(rng, 4).value = dgAutobuses.Rows(i).Cells(4).Value
            wb.SHEETS(1).cells(rng, 5).value = dgAutobuses.Rows(i).Cells(5).Value.ToString.Trim
            wb.SHEETS(1).cells(rng, 6).value = dgAutobuses.Rows(i).Cells(6).Value.ToString.Trim
            wb.SHEETS(1).cells(rng, 7).value = dgAutobuses.Rows(i).Cells(7).Value.ToString.Trim
            rng = rng + 1
        Next

        wb.SHEETS(1).cells(1, 2).value = EmpresaActiva
        wb.SHEETS(1).cells(1, 2).FONT.SIZE = 20
        wb.SHEETS(1).cells(1, 2).FONT.name = "Segoe UI Light"
        wb.SHEETS(1).cells(2, 2).value = "Vista previa del Catálogo de Autobuses"
        wb.SHEETS(1).cells(2, 2).FONT.SIZE = 14
        wb.SHEETS(1).cells(2, 2).FONT.name = "Segoe UI Light"
        wb.SHEETS(1).cells(3, 2).value = "Impreso el " & Now().Day & "-" & Now().Month & "-" & Now().Year & " por " & UsuarioActivo
        wb.SHEETS(1).cells(3, 3).FONT.SIZE = 12
        wb.SHEETS(1).cells(3, 3).FONT.name = "Segoe UI Light"

        wb.SHEETS(1).cells(1, 1).value = ""
        wb.SHEETS(1).columns(1).columnwidth = 3
        wb.sheets(1).cells(1, 1).interior.color = 15773696
        wb.sheets(1).cells(2, 1).interior.color = 15773696

    End Sub

    Private Sub ExportarFiltroAExcelToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExportarFiltroAExcelToolStripMenuItem.Click
        Bitacora.RegistraActividad("Imprimió el filtro activo del catálogo de autobuses")
        ImprimeFiltro()

    End Sub

    Private Sub ToolStripButton2_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton2.Click
        Bitacora.RegistraActividad("Imprimió el filtro activo del catálogo de autobuses")
        ImprimeFiltro()

    End Sub

    Private Sub ArchivoToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        frmCatalogoClients.ShowDialog()

    End Sub
End Class